package com.mjh.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.mjh.bean.Discuss;
import com.mjh.comm.DBInstance;

public class DiscussDAO {

	public static Logger log = Logger.getLogger(DiscussDAO.class);
	
	public static final String NAME_SPACE = "com.mjh.dao.mapper.discuss";
	
	public int insert(Discuss discuss){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.insert(NAME_SPACE + ".insert", discuss);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}

	public int updateDiscussId(int discussId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.insert(NAME_SPACE + ".updateDiscussId", discussId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public int delete(int id){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".del", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;		
	}

	public int deleteByArticle(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".delByArticle", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;		
	}
	
	public Discuss select(int id) {
		SqlSession session = DBInstance.getInstance().getSession();
		Discuss discuss = null;
		try {
			discuss = (Discuss) session.selectOne(NAME_SPACE + ".selectOne", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return discuss;
	}
	
	@SuppressWarnings("unchecked")    
	public List<Discuss> selectForList(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		List<Discuss> articles = new ArrayList<Discuss>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectForList", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return articles;
	}
	
	public int selectCount(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return  (Integer)session.selectOne(NAME_SPACE + ".selectCount", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return 0;
	}	
	
	public static void main(String[] args) {
		Discuss d = new Discuss();
		d.setName("2222");
		d.setEmail("eeeeeeee");
		d.setContent("333333333");
		DiscussDAO dao = new DiscussDAO();
		System.out.println(dao.insert(d));
		System.out.println(d.getId());
	}
}
